﻿Imports Entities
Imports DataAccess

Public Class GarantiaBL
    Public Shared Function List() As List(Of GARANTIAS)
        Dim lista As New List(Of GARANTIAS)
        lista = GarantiaDAL.List
        Return lista
    End Function

    Public Shared Function ListGarantiasCambiadas() As List(Of ClienteBalonCambiado)
        Dim listaF As New List(Of ClienteBalonCambiado)
        Dim lista = GarantiasCambiadasDAL.List
        If Not IsNothing(lista) Then
            For Each rec In lista
                Dim cliente = ClienteBL.SearchById(rec.CLIENTE)
                Dim clientep = ClienteBL.ClientesToClientesP(cliente)
                Dim venta = VentaBL.SearchById(rec.Venta)
                Dim detalles = VentaBL.GetDetalleR(venta.VENTA)
                Dim garantiaCambiada = GarantiaBL.SearchById(rec.GARANTIACAMBIADAID)
                Dim boleta As New boletas
                Dim factura As New facturasventa
                If venta.BOLETA > 0 Then
                    boleta = BoletaBL.SearchById(venta.BOLETA)
                Else
                    factura = FacturaVentaBL.SearchById(venta.FACTURAID)
                End If
                Dim obj As New ClienteBalonCambiado

                With obj
                    .ClienteId = cliente.CLIENTE
                    .Numero = clientep.Numero
                    .Calle = clientep.Calle
                    .Villa = clientep.Villa
                    .Rut = clientep.Rut
                    .Nombre = clientep.Nombre
                    .Fono = clientep.Fono
                    .FechaVenta = venta.FECHA.ToString
                    .VentaId = venta.VENTA
                    .GarantiaCambiadaId = rec.GARANTIACAMBIADAID
                    .GarantiaCambiada = GetDescripcion(.GarantiaCambiadaId)
                    .GarantiaVentaId = rec.GARANTIAVENTAID
                    .GarantiaVenta = GetDescripcion(.GarantiaVentaId)
                    If boleta.BOLETA > 0 Then .Descripcion = boleta.DESCRIPCION Else .Descripcion = factura.DESCRIPCION
                End With
                listaF.Add(obj)
            Next
        End If
        If listaF.Count > 0 Then
            Return listaF
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function Save(ByVal zObject As GARANTIAS) As Boolean
        If GarantiaDAL.Exist(zObject.GARANTIA) Then
            GarantiaDAL.Update(zObject)
            Return True
        Else
            Return GarantiaDAL.Create(zObject)
        End If
    End Function
  

    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Return GarantiaDAL.Delete(zId)
    End Function

    Public Shared Function SearchById(ByVal zId As Long) As GARANTIAS
        Dim obj As New GARANTIAS
        obj = GarantiaDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function UpdateStock(ByVal zId As Long, ByVal zDecrementoStock As Long, Optional ByVal zPrecio As Long = 0) As Boolean
        Dim obj As New GARANTIAS
        obj = GarantiaDAL.SearchById(zId)

        If Not IsNothing(obj) Then
            If zPrecio > 0 Then obj.PRECIOVENTA = zPrecio
            obj.STOCK -= zDecrementoStock
            
            GarantiaDAL.Update(obj)
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function CambioGarantia(zventaId As Long, ByVal zGarantiaVentaId As Long, ByVal zgarantiaCambiadaId As Long) As Boolean
        UpdateStock(zGarantiaVentaId, -1)
        UpdateStock(zgarantiaCambiadaId, 1)

        Return GarantiasCambiadasDAL.Delete(zventaId)
    End Function

    Public Shared Function SetStock(ByVal zId As Long, ByVal zStock As Long) As GARANTIAS
        Dim obj As New GARANTIAS
        obj = GarantiaDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            obj.STOCK = zStock
            Return GarantiaDAL.Update(obj)
        Else
            Return Nothing
        End If
    End Function
    Public Shared Function UpdateStockMasivo(ByVal zId As Long, ByVal zIncrementoStock As Long) As Boolean
        Dim obj As New garantias
        obj = GarantiaDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            obj.STOCK += zIncrementoStock
            GarantiaDAL.Update(obj)
            Return True
        Else
            Return False
        End If
    End Function
  
    Public Shared Function GetDescripcion(ByVal zId As Long) As String
        Dim obj As New GARANTIAS
        obj = GarantiaDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj.DESCRIPCION
        Else
            Return ""
        End If
    End Function
    Public Shared Function GetKilos(ByVal zId As Long) As Long
        Dim obj As New GARANTIAS
        obj = GarantiaDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj.CAPACIDAD
        Else
            Return 0
        End If
    End Function
    Public Shared Function GetStock(ByVal zId As Long) As Long
        Dim obj As New GARANTIAS
        obj = GarantiaDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj.STOCK
        Else
            Return 0
        End If
    End Function
   
    Public Shared Function GetGarantiasDisponibles(ByVal zGarantiaId As Long) As Long
        'SUMAR LA CANTIDAD DE CARGAS Y RESTARLAS AL STOCK TOTAL  DE LA GARANTIA
        Dim obj As New GARANTIAS
        Dim tot As Integer = 0
        obj = GarantiaDAL.SearchById(zGarantiaId)
        If Not IsNothing(obj) Then
            Dim lista = ItemBL.ListByGarantiaId(obj.GARANTIA)
            For Each rec In lista
                tot += rec.STOCK
            Next
            Return obj.STOCK - tot
        Else
            Return Nothing
        End If
    End Function
End Class
